﻿namespace AddressBook.Web
{
  #region "Using Statements"
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;

  using Business.Person;
  using Business.Contact;
  #endregion

  public partial class Default : System.Web.UI.Page
	{
		#region "Page Events"

		protected void Page_Init(object sender, EventArgs e)
		{
			if (!this.IsPostBack)
			{
				this.BindContactGrid();
        this.BindAddressData();
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{

		}

		#endregion

		#region "Bind Functions"

		private void BindContactGrid()
		{
			ContactList contacts;

			if (this.ViewState["SearchCriteria"] == null || String.IsNullOrEmpty(this.ViewState["SearchCriteria"].ToString()))
			{
				contacts = ContactList.GetContactList();
			}
			else
			{
				contacts = ContactList.GetContactList(this.ViewState["SearchCriteria"].ToString());
			}

			this.DefaultContactGrid.DataSource = contacts;
			this.DefaultContactGrid.DataBind();
		}

		private void BindAddressList(Contact contact)
		{
			this.DefaultEditContactAddressListBox.DataSource = contact.Addresses;
			this.DefaultEditContactAddressListBox.DataTextField = "FullAddress";
			this.DefaultEditContactAddressListBox.DataValueField = "AddressId";
			this.DefaultEditContactAddressListBox.DataBind();

			List<int> addressTypeIds = (from a in contact.Addresses
																	select a.AddressTypeId).ToList<int>();

			AddressTypeList addressTypeList = AddressTypeList.GetList();
			var addressTypes = from at in addressTypeList
												 where !addressTypeIds.Contains(at.Key)
												 select at;

			this.AddAddressTypeDropDown.DataSource = addressTypes;
			this.AddAddressTypeDropDown.DataValueField = "Key";
			this.AddAddressTypeDropDown.DataTextField = "Value";
			this.AddAddressTypeDropDown.DataBind();
			this.AddAddressTypeDropDown.SelectedIndex = -1;

			this.EditAddressTypeDropDown.DataSource = addressTypeList;
			this.EditAddressTypeDropDown.DataValueField = "Key";
			this.EditAddressTypeDropDown.DataTextField = "Value";
			this.EditAddressTypeDropDown.DataBind();
			this.EditAddressTypeDropDown.SelectedIndex = -1;

			AddressList addresses = AddressList.GetAddressList();
			this.AddAddressExistingDropDown.DataSource = addresses;
			this.AddAddressExistingDropDown.DataValueField = "Id";
			this.AddAddressExistingDropDown.DataTextField = "FullAddress";
      this.AddAddressExistingDropDown.DataBind();
		}

		private void BindPhoneList(Contact contact)
		{
			this.EditContactPhoneListBox.DataSource = contact.PhoneNumbers;
			this.EditContactPhoneListBox.DataTextField = "DisplayPhoneNumber";
			this.EditContactPhoneListBox.DataValueField = "PhoneNumberId";
			this.EditContactPhoneListBox.DataBind();

			List<int> phoneTypeIds = (from p in contact.PhoneNumbers
																select p.PhoneNumberTypeId).ToList<int>();

			PhoneNumberTypeList phoneTypesList = PhoneNumberTypeList.GetList();
			var phoneTypes = from pt in phoneTypesList
											 where !phoneTypeIds.Contains(pt.Key)
											 select pt;

			this.AddPhoneNumberTypeDropDown.DataSource = phoneTypes;
			this.AddPhoneNumberTypeDropDown.DataValueField = "Key";
			this.AddPhoneNumberTypeDropDown.DataTextField = "Value";
			this.AddPhoneNumberTypeDropDown.DataBind();
			this.AddPhoneNumberTypeDropDown.SelectedIndex = -1;

			this.EditPhoneTypeDropDown.DataSource = phoneTypesList;
			this.EditPhoneTypeDropDown.DataValueField = "Key";
			this.EditPhoneTypeDropDown.DataTextField = "Value";
			this.EditPhoneTypeDropDown.DataBind();
			this.EditPhoneTypeDropDown.SelectedIndex = -1;
		}

    private void BindEmailList(Contact contact)
    {
      this.EditContactEmailListBox.DataSource = contact.EmailAddresses;
      this.EditContactEmailListBox.DataTextField = "EmailAddress";
      this.EditContactEmailListBox.DataValueField = "EmailAddressId";
      this.EditContactEmailListBox.DataBind();

      List<int> emailTypeIds = (from e in contact.EmailAddresses
                                select e.EmailAddressTypeId).ToList<int>();

      EmailAddressTypeList emailTypesList = EmailAddressTypeList.GetList();
      var emailTypes = from et in emailTypesList
                       where !emailTypeIds.Contains(et.Key)
                       select et;

      this.AddEmailTypeDropDown.DataSource = emailTypes;
      this.AddEmailTypeDropDown.DataValueField = "Key";
      this.AddEmailTypeDropDown.DataTextField = "Value";
      this.AddEmailTypeDropDown.DataBind();
      this.AddEmailTypeDropDown.SelectedIndex = -1;

      this.EditEmailTypeDropDown.DataSource = emailTypesList;
      this.EditEmailTypeDropDown.DataValueField = "Key";
      this.EditEmailTypeDropDown.DataTextField = "Value";
      this.EditEmailTypeDropDown.DataBind();
      this.EditEmailTypeDropDown.SelectedIndex = -1;
    }

		private void BindAddressData()
		{
			StateProvinceList states = StateProvinceList.GetStateProvinceList();
			this.AddAddressStateDropDown.DataSource = states;
			this.AddAddressStateDropDown.DataValueField = "Id";
			this.AddAddressStateDropDown.DataTextField = "StateProvinceCode";
			this.AddAddressStateDropDown.DataBind();
			this.AddAddressStateDropDown.SelectedIndex = -1;

			this.EditAddressStateDropDown.DataSource = states;
			this.EditAddressStateDropDown.DataValueField = "Id";
			this.EditAddressStateDropDown.DataTextField = "StateProvinceCode";
			this.EditAddressStateDropDown.DataBind();
			this.EditAddressStateDropDown.SelectedIndex = -1;
		}

		#endregion

		#region "ContactGrid Events"

		protected void DefaultSearchImageButton_Click(object sender, ImageClickEventArgs e)
		{
			this.ViewState["SearchCriteria"] = this.DefaultSearchTextBox.Text;

			this.BindContactGrid();
		}

		protected void DefaultClearSearchImageButton_Click(object sender, ImageClickEventArgs e)
		{
			this.DefaultSearchTextBox.Text = String.Empty;
			this.ViewState["SearchCriteria"] = String.Empty;

			this.BindContactGrid();
		}

		protected void DefaultContactGrid_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			switch (e.CommandName)
			{
				case "Edit":
					Contact contact = Contact.GetContact(int.Parse(e.CommandArgument.ToString()));

					this.DefaultEditContactIdField.Value = contact.Id.ToString();
					this.DefaultEditContactTitleTextBox.Text = contact.Title;
					this.DefaultEditContactFirstNameTextBox.Text = contact.FirstName;
					this.DefaultEditContactMiddleNameTextBox.Text = contact.MiddleName;
					this.DefaultEditContactLastNameTextBox.Text = contact.LastName;
					this.DefaultEditContactSuffixTextBox.Text = contact.Suffix;

					this.BindAddressList(contact);
					this.BindPhoneList(contact);
          this.BindEmailList(contact);

					this.DefaultContactMultiView.SetActiveView(this.DefaultContactEditView);
					break;

				case "Delete":
					Contact.DeleteContact(int.Parse(e.CommandArgument.ToString()));

					this.BindContactGrid();
					break;
			}
		}

		protected void DefaultContactGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
		{
			this.DefaultContactGrid.PageIndex = e.NewPageIndex;
			this.BindContactGrid();
		}

		protected void DefaultContactGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
		{
			
		}

		protected void DefaultContactGrid_RowEditing(object sender, GridViewEditEventArgs e)
		{

		}

		#endregion

		#region "Contact Button Events"

		protected void DefaultAddContactSaveButton_Click(object sender, EventArgs e)
		{
			Contact contact = Contact.NewContact();

			contact.Title = this.DefaultAddContactTitleTextBox.Text;
			contact.FirstName = this.DefaultAddContactFirstNameTextBox.Text;
			contact.MiddleName = this.DefaultAddContactMiddleNameTextBox.Text;
			contact.LastName = this.DefaultAddContactLastNameTextBox.Text;
			contact.Suffix = this.DefaultAddContactSuffixTextBox.Text;

			contact = contact.Save();

      this.DefaultAddContactTitleTextBox.Text = String.Empty;
      this.DefaultAddContactFirstNameTextBox.Text = String.Empty;
      this.DefaultAddContactMiddleNameTextBox.Text = String.Empty;
      this.DefaultAddContactLastNameTextBox.Text = String.Empty;
      this.DefaultAddContactSuffixTextBox.Text = String.Empty;

			this.BindContactGrid();
		}

		protected void DefaultAddContactCloseButton_Click(object sender, ImageClickEventArgs e)
		{
			this.DefaultAddContactTitleTextBox.Text = String.Empty;
			this.DefaultAddContactFirstNameTextBox.Text = String.Empty;
			this.DefaultAddContactMiddleNameTextBox.Text = String.Empty;
			this.DefaultAddContactLastNameTextBox.Text = String.Empty;
			this.DefaultAddContactSuffixTextBox.Text = String.Empty;
		}

		protected void DefaultEditContactSaveButton_Click(object sender, EventArgs e)
		{
			Contact contact = Contact.GetContact(int.Parse(this.DefaultEditContactIdField.Value));

			contact.Title = this.DefaultEditContactTitleTextBox.Text;
			contact.FirstName = this.DefaultEditContactFirstNameTextBox.Text;
			contact.MiddleName = this.DefaultEditContactMiddleNameTextBox.Text;
			contact.LastName = this.DefaultEditContactLastNameTextBox.Text;
			contact.Suffix = this.DefaultEditContactSuffixTextBox.Text;

			contact = contact.Save();

      this.DefaultEditContactIdField.Value = String.Empty;
      this.DefaultEditContactTitleTextBox.Text = String.Empty;
      this.DefaultEditContactFirstNameTextBox.Text = String.Empty;
      this.DefaultEditContactMiddleNameTextBox.Text = String.Empty;
      this.DefaultEditContactLastNameTextBox.Text = String.Empty;
      this.DefaultEditContactSuffixTextBox.Text = String.Empty;
      this.DefaultEditContactAddressListBox.DataSource = null;

			this.BindContactGrid();
      this.DefaultContactMultiView.SetActiveView(this.DefaultContactGridView);
		}

    protected void DefaultEditContactCloseButton_Click(object sender, ImageClickEventArgs e)
    {
      this.DefaultEditContactIdField.Value = String.Empty;
      this.DefaultEditContactTitleTextBox.Text = String.Empty;
      this.DefaultEditContactFirstNameTextBox.Text = String.Empty;
      this.DefaultEditContactMiddleNameTextBox.Text = String.Empty;
      this.DefaultEditContactLastNameTextBox.Text = String.Empty;
      this.DefaultEditContactSuffixTextBox.Text = String.Empty;
      this.DefaultEditContactAddressListBox.DataSource = null;

      this.BindContactGrid();
      this.DefaultContactMultiView.SetActiveView(this.DefaultContactGridView);
		}

		#endregion

		#region "Address Button Events"

		protected void AddAddressFromExisting_CheckedChanged(object sender, EventArgs e)
		{
      if (this.AddAddressFromExisting.Checked)
      {
        this.EditContactAddAddressPanelPopupExtender.Show();
        AddressInfo address = AddressList.GetAddressList().FirstOrDefault(a => a.Id == int.Parse(this.AddAddressExistingDropDown.SelectedValue));

        this.AddAddressLine1TextBox.Text = address.Address1;
        this.AddAddressLine2TextBox.Text = address.Address2;
        this.AddAddressCityTextBox.Text = address.City;
        this.AddAddressStateDropDown.SelectedValue = address.StateProvinceId.ToString();
        this.AddAddressPostcodeTextBox.Text = address.PostalCode;

        this.AddAddressExistingDropDown.Visible = true;
        this.AddAddressExistingDropDown.Enabled = true;
      }
      else
      {
        this.EditContactAddAddressPanelPopupExtender.Show();
        this.AddAddressLine1TextBox.Text = String.Empty;
        this.AddAddressLine2TextBox.Text = String.Empty;
        this.AddAddressCityTextBox.Text = String.Empty;
        this.AddAddressStateDropDown.SelectedIndex = -1;
        this.AddAddressPostcodeTextBox.Text = String.Empty;
        this.AddAddressTypeDropDown.SelectedIndex = -1;

        this.AddAddressExistingDropDown.Visible = false;
        this.AddAddressExistingDropDown.Enabled = false;
      }
		}

    protected void AddAddressExistingDropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
      this.EditContactAddAddressPanelPopupExtender.Show();
      AddressInfo address = AddressList.GetAddressList().FirstOrDefault(a => a.Id == int.Parse(this.AddAddressExistingDropDown.SelectedValue));

      this.AddAddressLine1TextBox.Text = address.Address1;
      this.AddAddressLine2TextBox.Text = address.Address2;
      this.AddAddressCityTextBox.Text = address.City;
      this.AddAddressStateDropDown.SelectedValue = address.StateProvinceId.ToString();
      this.AddAddressPostcodeTextBox.Text = address.PostalCode;
    }

		protected void AddAddressSaveButton_Click(object sender, ImageClickEventArgs e)
    {
      Contact contact = Contact.GetContact(int.Parse(this.DefaultEditContactIdField.Value));
      contact.Addresses.Add(
        this.AddAddressLine1TextBox.Text,
        this.AddAddressLine2TextBox.Text,
        this.AddAddressCityTextBox.Text,
        int.Parse(this.AddAddressStateDropDown.SelectedValue),
        this.AddAddressPostcodeTextBox.Text,
        int.Parse(this.AddAddressTypeDropDown.SelectedValue));

      contact = contact.Save();

      this.BindAddressList(contact);

      this.AddAddressLine1TextBox.Text = String.Empty;
      this.AddAddressLine2TextBox.Text = String.Empty;
      this.AddAddressCityTextBox.Text = String.Empty;
      this.AddAddressStateDropDown.SelectedIndex = -1;
      this.AddAddressPostcodeTextBox.Text = String.Empty;
      this.AddAddressTypeDropDown.SelectedIndex = -1;
    }

    protected void AddressEditImageButton_Click(object sender, ImageClickEventArgs e)
    {
      Contact contact = Contact.GetContact(int.Parse(this.DefaultEditContactIdField.Value));
      ContactAddress address = contact.Addresses.GetItem(int.Parse(this.DefaultEditContactAddressListBox.SelectedValue));

      this.EditAddressIdField.Value = address.AddressId.ToString();

      this.EditAddressLine1TextBox.Text = address.Address1;
      this.EditAddressLine2TextBox.Text = address.Address2;
      this.EditAddressCityTextBox.Text = address.City;
      this.EditAddressStateDropDown.SelectedValue = address.StateProvinceId.ToString();
      this.EditAddressPostcodeTextBox.Text = address.PostalCode;
      this.EditAddressTypeDropDown.SelectedValue = address.AddressTypeId.ToString();

      this.EditContactEditAddressPanelPopupExtender.Show();
    }

    protected void EditAddressSaveButton_Click(object sender, ImageClickEventArgs e)
    {
      Contact contact = Contact.GetContact(int.Parse(this.DefaultEditContactIdField.Value));
      ContactAddress address = contact.Addresses.GetItem(int.Parse(this.EditAddressIdField.Value));

      address.Address1 = this.EditAddressLine1TextBox.Text;
      address.Address2 = this.EditAddressLine2TextBox.Text;
      address.City = this.EditAddressCityTextBox.Text;
      address.StateProvinceId = int.Parse(this.EditAddressStateDropDown.SelectedValue);
      address.PostalCode = this.EditAddressPostcodeTextBox.Text;
      address.AddressTypeId = int.Parse(this.EditAddressTypeDropDown.SelectedValue);

      contact = contact.Save();

      this.BindAddressList(contact);
      this.BindContactGrid();

      this.EditAddressIdField.Value = address.AddressId.ToString();

      this.EditAddressLine1TextBox.Text = String.Empty;
      this.EditAddressLine2TextBox.Text = String.Empty;
      this.EditAddressCityTextBox.Text = String.Empty;
      this.EditAddressStateDropDown.SelectedIndex = -1;
      this.EditAddressPostcodeTextBox.Text = String.Empty;
      this.EditAddressTypeDropDown.SelectedIndex = -1;
    }

    protected void AddressDeleteImageButton_Click(object sender, ImageClickEventArgs e)
    {
      Contact contact = Contact.GetContact(int.Parse(this.DefaultEditContactIdField.Value));
      ContactAddress address = contact.Addresses.GetItem(int.Parse(this.DefaultEditContactAddressListBox.SelectedValue));

      contact.Addresses.Remove(address);
      contact = contact.Save();

      this.BindAddressList(contact);
		}

		#endregion

		#region "Phone Button Events"

		protected void AddPhoneSaveImageButton_Click(object sender, ImageClickEventArgs e)
		{
			Contact contact = Contact.GetContact(int.Parse(this.DefaultEditContactIdField.Value));
			contact.PhoneNumbers.Add(
				PhoneNumber.UnFormatPhone(this.AddPhoneNumberTextBox.Text),
				int.Parse(this.AddPhoneNumberTypeDropDown.SelectedValue));

			contact = contact.Save();

			this.BindPhoneList(contact);

			this.AddPhoneNumberTextBox.Text = String.Empty;
			this.AddPhoneNumberTypeDropDown.SelectedIndex = -1;
		}

		protected void PhoneEditImageButton_Click(object sender, ImageClickEventArgs e)
		{
			Contact contact = Contact.GetContact(int.Parse(this.DefaultEditContactIdField.Value));
			ContactPhoneNumber phone = contact.PhoneNumbers.GetItem(int.Parse(this.EditContactPhoneListBox.SelectedValue));

			this.EditAddressIdField.Value = phone.PhoneNumberId.ToString();

			this.EditPhoneTextBox.Text = phone.FormattedPhoneNumber;
			this.EditPhoneTypeDropDown.SelectedValue = phone.PhoneNumberTypeId.ToString();
			this.EditPhoneTypeDropDown.Enabled = false;

			this.EditContactEditPhonePanelPopupExtender.Show();
		}

		protected void EditPhoneSaveImageButton_Click(object sender, ImageClickEventArgs e)
		{
			Contact contact = Contact.GetContact(int.Parse(this.DefaultEditContactIdField.Value));
			ContactPhoneNumber phone = contact.PhoneNumbers.GetItem(int.Parse(this.EditPhoneIdField.Value));

			phone.PhoneNumber = PhoneNumber.UnFormatPhone(this.EditPhoneTextBox.Text);

			contact = contact.Save();

			this.BindPhoneList(contact);
			this.BindContactGrid();

			this.EditPhoneTextBox.Text = String.Empty;
			this.EditPhoneTypeDropDown.SelectedIndex = -1;
		}

		protected void PhoneDeleteImageButton_Click(object sender, ImageClickEventArgs e)
		{
			Contact contact = Contact.GetContact(int.Parse(this.DefaultEditContactIdField.Value));
			ContactPhoneNumber phone = contact.PhoneNumbers.GetItem(int.Parse(this.EditContactPhoneListBox.SelectedValue));

			contact.PhoneNumbers.Remove(phone.PhoneNumberId);
			contact = contact.Save();

			this.BindPhoneList(contact);
		}
		#endregion

    #region "Email Button Events"

    protected void AddEmailSaveImageButton_Click(object sender, ImageClickEventArgs e)
    {
      Contact contact = Contact.GetContact(int.Parse(this.DefaultEditContactIdField.Value));
      contact.EmailAddresses.Add(
        this.AddEmailTextBox.Text,
        int.Parse(this.AddEmailTypeDropDown.SelectedValue));

      contact = contact.Save();

      this.BindEmailList(contact);

      this.AddEmailTextBox.Text = String.Empty;
      this.AddEmailTypeDropDown.SelectedIndex = -1;
    }

    protected void EmailEditImageButton_Click(object sender, ImageClickEventArgs e)
    {
      Contact contact = Contact.GetContact(int.Parse(this.DefaultEditContactIdField.Value));
      ContactEmailAddress email = contact.EmailAddresses.GetItem(int.Parse(this.EditContactEmailListBox.SelectedValue));

      this.EditEmailIdField.Value = email.EmailAddressId.ToString();

      this.EditEmailTextBox.Text = email.EmailAddress;
      this.EditEmailTypeDropDown.SelectedValue = email.EmailAddressTypeId.ToString();
      this.EditEmailTypeDropDown.Enabled = false;

      this.EditContactEditEmailPanelPopupExtender.Show();
    }

    protected void EditEmailSaveImageButton_Click(object sender, ImageClickEventArgs e)
    {
      Contact contact = Contact.GetContact(int.Parse(this.DefaultEditContactIdField.Value));
      ContactEmailAddress email = contact.EmailAddresses.GetItem(int.Parse(this.EditEmailIdField.Value));

      email.EmailAddress = this.EditEmailTextBox.Text;

      contact = contact.Save();

      this.BindEmailList(contact);

      this.EditEmailTextBox.Text = String.Empty;
      this.EditEmailTypeDropDown.SelectedIndex = -1;
    }

    protected void EmailDeleteImageButton_Click(object sender, ImageClickEventArgs e)
    {
      Contact contact = Contact.GetContact(int.Parse(this.DefaultEditContactIdField.Value));
      ContactEmailAddress email = contact.EmailAddresses.GetItem(int.Parse(this.EditContactEmailListBox.SelectedValue));

      contact.EmailAddresses.Remove(email.EmailAddressId);
      contact = contact.Save();

      this.BindEmailList(contact);
    }

    #endregion
	}
}
